package com.burgeon.backend.EnterpriseWeChat.rest;

import com.burgeon.backend.EnterpriseWeChat.entity.RFIMVipCount;
import com.burgeon.backend.EnterpriseWeChat.entity.SaleData;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 会员等级转换
 */
public class FindRFIMByStoreId extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindRFIMByStoreId(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public ArrayList<RFIMVipCount>  getData(String storeId) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(storeId), url);
//        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(new JSONArray(str).getJSONObject(0).getString("message"));
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<RFIMVipCount> rfimVipCounts = new ArrayList<>();
        for (int i=0;i<jsonArray.length();i++) {
            RFIMVipCount rfimVipCount = new RFIMVipCount();
            if(jsonArray.length()>0){
                rfimVipCount.setDescription(jsonArray.getJSONArray(i).getString(0));
                rfimVipCount.setQty(jsonArray.getJSONArray(i).getString(1));
                rfimVipCounts.add(rfimVipCount);
            }
        }


        return rfimVipCounts;
    }

    private MultiValueMap Data(String storeId) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("DESCRIPTION");//描述
        colums.add("QTY");//数量

        params.put("columns", colums);
        params.put("table", "RFIM_VIPCOUNT_UD");//表名
        params.put("count", "true");

        HashMap paramss = new HashMap();
        paramss.put("column", "'C_STORE_ID'");
        paramss.put("condition", "'"+storeId+"'");

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindRFIMByStoreId g = new FindRFIMByStoreId("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(g.getData(""));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
